<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{   
    public function _initialize(){
       if(!session('?islogin')){
        $this->error('请先登录',U('User/login'));
       }
    }

    public function index()
    {
        $a                   = I('get.');
        $min                 = !empty($a['minage']) ? $a['minage'] : 0;
        $max                 = !empty($a['maxage']) ? $a['maxage'] : 1000;
        $conditions['age']   = array(array('gt', $min), array('lt', $max));
        $conditions['name']  = empty($a['name']) ? ['like', '%'] : ['like', '%' . $a['name'] . '%'];
        $conditions['sex']   = empty($a['sex']) ? ['like', '%'] : $a['sex'];
        $conditions['class'] = empty($a['class']) ? ['like', '%'] : $a['class'];
        //dump($conditions);
        $data  = M('stu');
        $count = $data->where($conditions)->count();
        $Page  = new \Think\Page($count, 5);
        $res   = $data->where($conditions)->order('id')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        //echo $data->getLastSql();
        $show = $Page->show();
        $this->assign('stu', $res);

        if(IS_AJAX){
            if($res && I('get.p')>0){
                $return['data'] = $this->fetch('ajaxPage');
                $return['info'] = '查询成功';
                $return['status'] = 1;
            }else{
                $return['data'] = 'null';
                $return['info'] = '查询失败';
                $return['status'] = 0;
            }
            $this->ajaxReturn($return);
        }


        $this->assign('page', $show);
        $this->display();
    }

    public function del($id)
    {
        $res = M('stu')->delete($id);
        if ($res) {
            $this->success('删除成功', U('Home/Index/index'));
        } else {
            $this->error('删除失败');

        }

    }

    public function add()
    {
        $this->display();
    }

    public function doadd()
    {

        $res = M('stu')->add(I('post.'));
        if ($res) {
            $this->success('添加成功', U('Home/Index/index'));

        } else {
            $this->error('添加失败');

        }
    }

    public function update($id)
    {
        $res = M('stu')->Find($id);
        $this->assign('student', $res);
        $this->display();

    }

    public function doupdate()
    {

        $res = M('stu')->where(['id' => I('post.id')])->save(I('post.'));
        if ($res) {
            $this->success('修改成功', U('Home/Index/index'));

        } else {
            $this->error('修改失败');

        }
    }


    public function uoload(){
        $config = array(
            'maxSize' =>  3145278,
            'rootPath'=> './Uploads'
            );
    }

   
}
